package android.ijiajiao.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import android.ijiajiao.dao.ArticleDao;
import android.ijiajiao.util.GetDateUtil;
import android.ijiajiao.util.JDBCConnectUtil;

public class ArticleDaoImpl implements ArticleDao {
	/**
	 * 
	 * 添加精彩文章 返回0为失败，返回1为成功
	 */
	@Override
	public int addArticle(String arti_autor, String arti_content, String arti_title) {
		// TODO Auto-generated method stub
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "insert into article (arti_title,arti_content,arti_autor,arti_time) values (?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, arti_title);
			ps.setString(2, arti_content);
			ps.setString(3, arti_autor);
			ps.setString(4, GetDateUtil.getdate());
			int row = ps.executeUpdate();
			if (row == 0) {
				return 0;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}
		return 1;
	}

	/**
	 * 精彩文章列表
	 */
	@Override
	public ArrayList<Object> ArticleList() {
		// TODO Auto-generated method stub
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select article.arti_id,article.arti_title,article.arti_autor,article.arti_time,user.img,article.collect_num from article  inner  join  user  on article.arti_autor=user.name order by article.arti_time desc";

		ArrayList<Object> data = new ArrayList<>();
		ArrayList<Object> list = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				list = new ArrayList<>();
				list.add(rs.getString(1));
				list.add(rs.getString(2));
				list.add(rs.getString(3));
				list.add(rs.getString(4));
				list.add(rs.getString(5));
				list.add(rs.getString(6));
				data.add(list);
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);

		}
		return data;

	}

	/**
	 * 精彩文章详情
	 */
	@Override
	public ArrayList<Object> ArticleDetail(String arti_id) {

		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		ArrayList<Object> list = new ArrayList<>();
		String sql = "select article.arti_content,article.arti_title,article.arti_autor,article.arti_time,user.img,article.collect_num from article  inner  join  user  on article.arti_autor=user.name where article.arti_id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, arti_id);
			rs = ps.executeQuery();
			while (rs.next()) {

				list.add(arti_id);
				list.add(rs.getString(1));
				list.add(rs.getString(2));
				list.add(rs.getString(3));
				list.add(rs.getString(4));
				list.add(rs.getString(5));
				list.add(rs.getString(6));

			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);

		}
		return list;

	}

	/**
	 * 删除精彩文章
	 */
	@Override
	public boolean DeleteArticle(String arti_id) {
		// TODO Auto-generated method stub

		boolean falg = false;
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "delete from article where arti_id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, arti_id);
			int row = ps.executeUpdate();
			System.out.println(row);
			if (row > 0) {
				falg = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}
		return falg;
	}

	@Override
	public ArrayList<String> collect(String name, String arti_id) {
		// TODO Auto-generated method stub
		ArrayList<String> data = new ArrayList<>();
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "insert into collect_arti(name,arti_id) values (?,?)";
		// update dynamic set dynamic_priase=dynamic_priase+1 where dynamic_id=
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, arti_id);
			int row = ps.executeUpdate();
			if (row > 0) {
				String sql1 = "update article set collect_num=collect_num+1 where arti_id=?";
				if (ps != null) {
					ps.close();
				}
				ps = conn.prepareStatement(sql1);
				ps.setString(1, arti_id);
				int row1 = ps.executeUpdate();
				if (row1 > 0) {
					String sql2 = "select collect_num from article where arti_id=?";
					if (ps != null) {
						ps.close();
					}
					ps = conn.prepareStatement(sql2);
					ps.setString(1, arti_id);
					rs = ps.executeQuery();
					if (rs.next()) {
						data.add(rs.getString(1));
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}
		return data;
	}

	@Override
	public ArrayList<Object> collected(String name) {
		// TODO Auto-generated method stub
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select arti_id,arti_title,arti_autor,arti_time,img,collect_num from article  inner  join user  on article.arti_autor=user.name and arti_id in (select arti_id from collect_arti where name=?) order by arti_time desc";
		ArrayList<Object> data = new ArrayList<>();
		ArrayList<Object> list = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				list = new ArrayList<>();
				list.add(rs.getString(1));
				list.add(rs.getString(2));
				list.add(rs.getString(3));
				list.add(rs.getString(4));
				list.add(rs.getString(5));
				list.add(rs.getString(6));
				data.add(list);
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);

		}
		return data;

	}

}
